
if (SERVER) then

	AddCSLuaFile( "shared.lua" )
	SWEP.Weight				= 5
	SWEP.AutoSwitchTo		= false
	SWEP.AutoSwitchFrom		= false
	SWEP.HoldType			= "shotgun"

end

if ( CLIENT ) then
	
	SWEP.ViewModelFOV		= 70
	
	SWEP.IconFont = "CSSelectIcons"
	
	SWEP.PrintName = "Winchester 1887"
	SWEP.Slot = 4
	SWEP.Slotpos = 0
	SWEP.IconLetter = "k"
	
	killicon.AddFont( "weapon_winchester_sh", "CSKillIcons", SWEP.IconLetter, Color( 255, 80, 0, 255 ) )

end

/*---------------------------------------------------------
   PRIMARY
   Semi Auto
---------------------------------------------------------*/

 SWEP.Base				= "tp_base"

 SWEP.Spawnable = true;
 SWEP.AdminSpawnable = true;

 SWEP.ViewModel			= "models/weapons/v_shot_m3super91.mdl"
 SWEP.WorldModel			= "models/weapons/w_annabelle.mdl"
 
 SWEP.IronSightsPos = Vector (4.7112, -4.0519, 1.7934)
 SWEP.IronSightsAng = Vector (0.3075, -0.1695, 0.9757)

 SWEP.Primary.Sound			= Sound( "weapons/shotgun/shotgun_fire6.wav" )
 SWEP.Primary.Recoil			= 25
 SWEP.Primary.Damage			= 5
 SWEP.Primary.VehDamageScale    = .1
 SWEP.Primary.NumShots		      = 15
 SWEP.Primary.Cone			= 0.09
 SWEP.Primary.ClipSize              = 5
 SWEP.Primary.Delay			= 0.75
 SWEP.Primary.Automatic             = false
 SWEP.Primary.Ammo                  = "buckshot"

 SWEP.Secondary.Clipsize            = -1;
 SWEP.Secondary.DefaultClip         = -1;
 SWEP.Secondary.Automatic           = false
 SWEP.Secondary.Ammo                = "none"; 
 SWEP.Secondary.Delay				= 0

function SWEP:PrimaryAttack()

	if ( self.Weapon:Clip1() <= 0 ) then 
		self.Weapon:EmitSound(Sound("weapons/shotgun/shotgun_empty.wav"),100,math.random(90,110)) 	
		if self.Owner:GetNWInt("ammo_"..self.Primary.Ammo, 0) <= 0 then return end
		if ( self.Weapon:GetNetworkedBool( "reloading", true ) ) then return end

		if ( self.Weapon:Clip1() <= self.Primary.ClipSize ) then
		
			self.Weapon:SetNetworkedBool( "Ironsights", false )
			self.Weapon:SetNetworkedBool( "reloading", true )
			self.Weapon:SetVar( "reloadtimer", CurTime() + 0.4 )
			self.Weapon:SendWeaponAnim( ACT_VM_RELOAD )
		
		end
	return end
	
	self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
	if  !self:CanPrimaryAttack()  then return end
	
	self.Weapon:SetNetworkedBool( "Ironsights", false )
	
	self:ShootBullets( self.Primary.Damage, self.Primary.NumShots, self.Primary.Cone)
	self.Owner:ViewPunch(Angle(math.Rand(-0.2,-0.1) * self.Primary.Recoil, math.Rand(-0.1,0.1) *self.Primary.Recoil, 0))
	self.Weapon:EmitSound(self.Primary.Sound,100,math.random(98,102))
	self.DoPump = CurTime() + .5
	self.Pumped = false
	self:TakePrimaryAmmo(1)
	self.Weapon:SetRecoilTime(CurTime() + .4)
end

function SWEP:ShootBullets( damage, numbullets, aimcone)

	local scale = 1
	local canscale = true
	if (self.Owner:KeyDown(IN_DUCK) or self.Owner:KeyDown(IN_WALK)) and canscale then
		if self.Owner:KeyDown(IN_FORWARD) or self.Owner:KeyDown(IN_BACK) or self.Owner:KeyDown(IN_MOVELEFT) or self.Owner:KeyDown(IN_MOVERIGHT) then
			scale = aimcone * 0.9
			canscale = false
		else
			scale = aimcone
			canscale = false
		end
	elseif (self.Owner:KeyDown(IN_SPEED) or self.Owner:KeyDown(IN_JUMP)) and canscale then
		scale = aimcone + (aimcone*1.2)
		canscale = false
	elseif (self.Owner:KeyDown(IN_FORWARD) or self.Owner:KeyDown(IN_BACK) or self.Owner:KeyDown(IN_MOVELEFT) or self.Owner:KeyDown(IN_MOVERIGHT)) and canscale then
		scale = aimcone + (aimcone*1.1)
	else
		scale = aimcone
	end
	
	local recoil = self.Weapon:GetRecoilRatio()
	scale = scale * recoil
	
	local bullet = {}
	bullet.Num 		= numbullets
	bullet.Src 		= self.Owner:GetShootPos()			
	bullet.Dir 		= self.Owner:GetAimVector()			
	bullet.Spread 	= Vector( scale, scale, 0 )		
	bullet.Tracer	= 1		
	bullet.Force	= math.Round(damage * 1.5)							
	bullet.Damage	= math.Round(damage * GameInfo.HumanDamageScale)
	bullet.AmmoType = "Pistol"
	bullet.TracerName 	= "Tracer"
	bullet.Callback = function ( attacker, tr, dmginfo )
		if tr.HitWorld or not tr.Entity then return end
		if not tr.Entity:IsPlayer() and not string.find(tr.Entity:GetClass(),"npc") then return end
		if tr.HitGroup == 1 and tr.Entity:IsValid() then
			local t2 = {}
			t2.start = tr.HitPos
			t2.endpos = self.Owner:GetAimVector() * 500
			t2.filter = tr.Entity
			local tr2 = util.TraceLine(t2)
			util.Decal("Blood", tr2.HitPos + tr2.HitNormal, tr2.HitPos - tr2.HitNormal)
			if math.random(1,8) == 1 then
				local effectdata = EffectData()
				effectdata:SetAngle(self.Owner:GetAimVector():Angle())
				effectdata:SetOrigin(tr.HitPos)
				util.Effect("head_gib", effectdata)
				local effectdata = EffectData()
				effectdata:SetEntity(tr.Entity)
				effectdata:SetNormal(tr.HitNormal)
				effectdata:SetOrigin(tr.HitPos)
				util.Effect("headshot", effectdata)
			end
			tr.Entity:EmitSound(ChooseRandom(GameSounds.HeadShot), 100, math.random(90,120))
		else
			util.Decal("Blood", tr.HitPos + tr.HitNormal, tr.HitPos - tr.HitNormal)
			if math.random(1,8) == 1 then
				local effectdata = EffectData()
				effectdata:SetOrigin(tr.HitPos)
				effectdata:SetNormal(tr.HitNormal)
				util.Effect("bodyshot", effectdata)
			end
			local t2 = {}
			t2.start = tr.HitPos
			t2.endpos = self.Owner:GetAimVector() * 500
			t2.filter = tr.Entity
			local tr2 = util.TraceLine(t2)
			util.Decal("Blood", tr2.HitPos + tr2.HitNormal, tr2.HitPos - tr2.HitNormal)
		end
	end
	
	self.Owner:FireBullets( bullet )
	
	self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK ) 		// View model animation
	self.Owner:MuzzleFlash()							// Crappy muzzle light
	self.Owner:SetAnimation( PLAYER_ATTACK1 )			// 3rd Person Animation
	
end

function SWEP:Deploy()
	self.Weapon:SendWeaponAnim(ACT_VM_DRAW)
	self.Weapon:SetNetworkedBool( "reloading", false )
end

function SWEP:Reload()
	if self.Owner:GetNWInt("ammo_"..self.Primary.Ammo, 0) <= self.Weapon:Clip1() then return end
	if self.Owner:GetNWInt("ammo_"..self.Primary.Ammo, 0) <= 0 then return end
	if ( self.Weapon:GetNetworkedBool( "reloading", true ) ) then return end

	if ( self.Weapon:Clip1() <= self.Primary.ClipSize ) then
		
		self.Weapon:SetNetworkedBool( "Ironsights", false )
		self.Weapon:SetNetworkedBool( "reloading", true )
		self.Weapon:SetVar( "reloadtimer", CurTime() + 0.4 )
		//self.Weapon:SendWeaponAnim( ACT_VM_RELOAD )
		
	end

return true end

SWEP.DoPump = 99999
function SWEP:Think()
	if self.DoPump < CurTime() and not self.Pumped then
		self:PumpIt()
		self.Pumped = true
	end
	if ( self.Weapon:GetNetworkedBool( "reloading", false ) ) then
		if ( self.Weapon:GetVar( "reloadtimer", 0 ) < CurTime() ) then
			
			// Finsished reload
			local size = math.min(self.Owner:GetNWInt("ammo_"..self.Primary.Ammo, 0),self.Primary.ClipSize)
			local clip1 = self.Weapon:Clip1() or 0
			if ( clip1 == size ) then
				self.Weapon:SendWeaponAnim( ACT_SHOTGUN_RELOAD_FINISH )
				self.Weapon:SetNetworkedBool( "reloading", false )
				return
			end
			
			// Next cycle
			self.Weapon:SetVar( "reloadtimer", CurTime() + 0.4 )
			self.Weapon:SetNextPrimaryFire( CurTime() + 0.5 )
			self.Weapon:SendWeaponAnim( ACT_VM_RELOAD )
			
			// Add ammo
			local clip = self.Weapon:Clip1()
			self.Weapon:SetClip1(  clip + 1 )
			
		end
	end
end

function SWEP:PumpIt()
	self.Weapon:SetNetworkedBool( "reloading", false )
	self.Weapon:SendWeaponAnim( ACT_SHOTGUN_PUMP )
	self.Weapon:EmitSound( Sound("weapons/m3/m3_pump.wav") )
	self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
end
